package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.entity.SysMenuData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysEmployeeServiceImpl;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Api(tags = "登录模块")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysEmployeeServiceImpl sysEmployeeServiceImpl;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        // 生成令牌
        return loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @ApiOperation("根据token获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        //Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        //Set<String> permissions = permissionService.getMenuPermission(user);
        // 部门集合
        List<Long> deptIdList = null;
        if(null != user.getDeptId()){
            deptIdList = sysEmployeeServiceImpl.getDeptIds(user.getDeptId());
        }
        // 网点集合
        List<String> outletsList = null;
        if(!Collections.isEmpty(deptIdList)){
            outletsList = sysEmployeeServiceImpl.getOutletsByDeptIds(deptIdList);
        }
        // 根据角色拿project - 通过userID获取roleID
        List<Long> roleIds =  sysEmployeeServiceImpl.getRoleIdByUserId(user.getUserId());
        List<Long> projectList = null;
        if(!Collections.isEmpty(roleIds)){
            projectList = sysEmployeeServiceImpl.getProjectByRoleIds(roleIds);
        }

        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        // ajax.put("roles", roles);
        ajax.put("depts", deptIdList);
        ajax.put("outlets", outletsList);
        ajax.put("projects", projectList);
        //ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    /*@ApiOperation("根据token获取用户信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        List<SysMenuData> menuData=menuService.buildMenusData(menus);
        return AjaxResult.success(menuService.buildMenus(menus));
    }*/

    @ApiOperation("根据token获取用户信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenuData> menuDataList=menuService.selectMenuDataTreeByUserId(user.getUserId());
        List<SysMenuData> menuData=menuService.buildMenusData(menuDataList);
        return AjaxResult.success(menuData);
    }

    /**
     * 获取小程序按钮
     * @return
     */
    @GetMapping(value = "/roleMenuTreeApplets")
    public AjaxResult roleMenuTreeApplets()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.roleMenuTreeApplets(user.getUserId());
        return AjaxResult.success(menus);
    }
}
